package com.hopu.dy.controller;

import com.hopu.dy.entity.R;
import com.hopu.dy.pojo.Person;
import com.hopu.dy.service.IPingLunService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pinglun")
public class PingLunController {

    @Resource
    private IPingLunService pingLunService;

    /**
     * 获取所有评论
     *
     * @return 评论列表
     */
    @GetMapping
    public R getAllPingLun() {
        List<Person> list=pingLunService.get();
        return R.ok(list);
    }

    //添加评论
    @PostMapping
    public R addPingLun(@RequestBody Person person) {
        // 设置默认值
        if (person.getImage() == null) {
            person.setImage("default_avatar.png");
        }
        if (person.getName() == null) {
            person.setName("匿名用户");
        }
        if (person.getCommentTime() == null) {
            person.setCommentTime(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
        }
        if (person.getLove() == null) {
            person.setLove(0L);
        }
        boolean result = pingLunService.addComment(person);
        return result ? R.ok("评论添加成功") : R.err("评论添加失败");
    }
    // 点赞
    @PostMapping("/love/{id}")
    public R love(@PathVariable long id) {
        boolean result = pingLunService.updateLoveNumber(id);
        if (result) {
            long newLoveCount=pingLunService.getLoveCount(id);
            return R.ok("点赞成功",newLoveCount);
        }
        return  R.err("点赞失败");
    }

}


